package com.asj.house.schedule;

import com.asj.house.entity.HouseIntermediary;
import com.asj.house.entity.HouseNotice;
import com.asj.house.entity.HouseRoom;
import com.asj.house.service.HouseIntermediaryService;
import com.asj.house.service.HouseNoticeService;
import com.asj.house.service.HouseRoomService;
import com.asj.house.utils.OSSUtil;
import com.asj.house.utils.TimeUtilCustomize;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Component
@Slf4j
public class OSSCleanJob {

    @Autowired
    private HouseRoomService houseRoomService;

    @Autowired
    private HouseIntermediaryService houseIntermediaryService;

    @Autowired
    private HouseNoticeService houseNoticeService;

    @Autowired
    private OSSUtil ossUtil;

    private String date = TimeUtilCustomize.parseDateToStr(TimeUtilCustomize.DATE_DAY_FORMAT, new Date());

    /**
     * 每天凌晨1点10清理 阿里云OSS服务器 house/room/文件夹下的垃圾图片
     */
    @Scheduled(cron = "0 10 1 * * ?")
    public void roomOSSCleanJob() {
        List<HouseRoom> houseRoomList = houseRoomService.list();

        //所有照片url集合
        List<String> urlList = houseRoomList.stream()
                .map(HouseRoom::getUrl)
                .collect(Collectors.toList());


        List<String> stringList = ossUtil.list("house/room");

        cleanOSS(urlList, stringList, "house/room/");
    }

    /**
     * 每天凌晨1点20清理 阿里云OSS服务器 house/head/文件夹下的垃圾图片
     */
    @Scheduled(cron = "0 20 1 * * ?")
    public void headOSSCleanJob() {
        List<HouseIntermediary> houseIntermediaryList = houseIntermediaryService.list();

        List<String> urlList = houseIntermediaryList.stream()
                .map(HouseIntermediary::getUrl)
                .collect(Collectors.toList());

        List<String> stringList = ossUtil.list("house/head");

        cleanOSS(urlList, stringList, "house/head/");
    }

    /**
     * 每天凌晨1点30清理 阿里云OSS服务器 house/news/文件夹下的垃圾图片
     */
    @Scheduled(cron = "0 30 1 * * ?")
    public void newsOSSCleanJob() {
        List<HouseNotice> houseNoticeList = houseNoticeService.list();

        List<String> urlList = houseNoticeList.stream()
                .map(HouseNotice::getUrl)
                .collect(Collectors.toList());

        List<String> stringList = ossUtil.list("house/news");

        cleanOSS(urlList, stringList, "house/news/");
    }


    /**
     * 阿里云OSS图片清理 公用方法
     * @param urlList
     * @param stringList
     * @param objectName
     */
    private void cleanOSS(List<String> urlList, List<String> stringList, String objectName) {
        if (Objects.isNull(stringList) || stringList.isEmpty()) {
            log.info(date + objectName + "文件夹下暂无垃圾图片清理");
            return;
        }

        stringList = stringList.stream()
                .filter(v -> !StringUtils.equals(v, objectName))
                .map(v -> "http://ansijia.cn/" + v)
                .collect(Collectors.toList());

        if (Objects.isNull(stringList) || stringList.isEmpty()) {
            log.info(date + objectName + "文件夹下暂无垃圾图片清理");
            return;
        }

        //两个集合取差集
        stringList.removeAll(urlList);

        if (Objects.isNull(stringList) || stringList.isEmpty()) {
            log.info(date + objectName + "文件夹下暂无垃圾图片清理");
            return;
        }

        stringList = stringList.stream()
                .map(v -> {
                    return v.split("http://ansijia.cn/")[1];
                })
                .collect(Collectors.toList());

        ossUtil.deleteAll(stringList);
        log.info(date + objectName + "文件夹下垃圾图片清理完毕！");
    }

}
